<?

	require_once('lib/OwnedObject.php');
	require_once('lib/ModelObject.php');
	
	define(SHOW_ALL, 		1);
	define(SHOW_ONLY_ADMIN, 2);

	class Category extends ModelObject {
			protected $modelName = 'Category';
			protected $title;
			protected $link;
			protected $parent;
			protected $hidden;
			protected $catorder;

			public static function Install() {
                    ModelObject::Install();
					$query=sprintf("CREATE TABLE `Category` (
									`Id` INT( 32 ) unsigned NOT NULL ,
									`title` VARCHAR(255) ,
									`link` VARCHAR(255) ,
									`parent` INT(32) ,
									`hidden` INT(2) ,
									`catorder` INT(32) ,
									PRIMARY KEY ( `Id` ),
									FOREIGN KEY (`Id`) REFERENCES `Object`(`id`) ON DELETE CASCADE
									) ENGINE=INNODB DEFAULT CHARSET=utf8;
								");
					mysql_query($query);

					$query = sprintf("INSERT INTO `ObjectType` SET `name` = '%s'", strval(__CLASS__));
                        mysql_query($query);
					echo "Category installed\n";
			}

			public function Load() {
                        parent::Load();

                        $query = sprintf("SELECT
												`title`, `link`, `parent`, `hidden`, `catorder`
										FROM
											`Category`
										WHERE
											`Id` =%d", $this->id);
                        $res = mysql_query($query);
                        $row = mysql_fetch_array($res);

                        $this->title = $row[0];
                        $this->link = $row[1];
                        $this->parent = $row[2];
                        $this->hidden = $row[3];
                        $this->catorder = $row[4];
			}

			public function AsSource($otherName = 'Category'){
						$result = parent::AsSource($otherName);

						$result -> Attribute('title',$this->title);
						$result -> Attribute('link',$this->link);

						$result -> Attribute('parent',$this->parent);
						$result -> Attribute('hidden',$this->hidden);
						$result -> Attribute('catorder',$this->catorder);
						
						$query = sprintf("SELECT `Id` FROM `Category` WHERE `parent`=%d ORDER BY `catorder`",$this->id);
						$rez = mysql_query($query);
						while ($rezult= mysql_fetch_assoc($rez)) {
							$cat_id = $rezult['Id'];
							$childCategory = new Category($cat_id);
							$childCategory->Load();
							$obj = $childCategory->AsSource();
							//echo "child ".$childCategory->Link()."<br/>";
							//echo "link ".replace_link($_SERVER['REQUEST_URI'])."<br/>";
							if(replace_link($_SERVER['REQUEST_URI']) == $childCategory->Link()){
								$obj -> Attribute('selected','selected');
								$result->Attribute('selected','selected');
							}
							
							$result->MountSource($obj);
						}
						if($this->parent == 0){
							$query = sprintf("SELECT `link`, `title` FROM `Article` WHERE `Category` = %d", $this->id);
							$art1 = mysql_query($query);
							while ($rez_art = mysql_fetch_assoc($art1)) {
								$art = new XmlSource('Category');
								$result->MountSource($art);
								$art->Attribute('title',$rez_art['title']);
								$art->Attribute('link','article/'.$rez_art['link']);
								$art->Attribute('hidden',1);
								if(replace_link($_SERVER['REQUEST_URI']) == "article/".$rez_art['link']){
									$art -> Attribute('selected','selected');
									$result->Attribute('selected','selected');
								}
							}
						}
						return $result;
			}

			public function Link($newLink=false) {
                        if($newLink)
                                $this->link = $newLink;
                        return $this->link;
            }

			public function CatParent($newParent=false) {
                        if($newParent)
                                $this->parent = $newParent;
                        return $this->parent;
            }
            
			public function Title($newTitle=false) {
                        if($newTitle)
                                $this->title = $newTitle;
                        return $this->title;
            }   
            
            public function Hidden($newHidden=false) {
                        if($newHidden)
                                $this->hidden = $newHidden;
                        return $this->hidden;
            }            
            
            public function Catorder($newCatorder=false) {
                        if($newCatorder)
                                $this->catorder = $newCatorder;
                        return $this->catorder;
            }            

			public function RegisterObject(){
						$obj_id= parent::RegisterObject();
						$query=sprintf("INSERT INTO
												`Category` ( `Id` , `title` , `link` , `parent`, `hidden`, `catorder` )
											VALUES (%d, '%s', '%s', %d, %d, %d)
							", $obj_id, mes($this->title), mes($this->link), $this->parent, $this->hidden, $this->catorder);
						mysql_query($query);
			}


			public function Save(){
						if($this->id<1){
							$this->RegisterObject();
						}
						else{
							$query=sprintf("UPDATE `Category`
											SET
												  `title` = '%s',
												  `link` = '%s',
												  `parent`= %d,
												  `hidden`= %d, 
												  `catorder` = %d
											WHERE `Id`=%d", mes($this->title), mes($this->link), $this->parent, $this->hidden, $this->catorder, $this->id);
							mysql_query($query);
						}
			}


			public function Remove(){
				$query=sprintf("DELETE FROM `Category` WHERE Id=%d",$this->id);
				mysql_query($query);
				parent::Remove();
			}
			

			
			//Навигация
			public static function Navigation(){
					$query = sprintf("SELECT `Id` FROM `Category` WHERE `parent`=0 ORDER BY `catorder`");
					$parse = mysql_query($query);
					
					$category_list = new XmlSource('Category-list');
					while($result = mysql_fetch_assoc($parse)){
						$c_id = $result['Id'];
						$category = new Category($c_id);
						$category->Load();
						
						
						$category_list->MountSource($category->AsSource());
					}
					
					return $category_list;
			}
			
			public static function AllParentCategoryes(){
			
				$query = "SELECT `Id`, `title` FROM `Category` WHERE `parent` = 0";
				$parse = mysql_query($query);
			
				$all = new XmlSource('all-categoryes');
				while($result = mysql_fetch_assoc($parse)){
					$item = new XmlSource('Category');
					$item->Attribute('id', $result['Id']);
					$item->Attribute('title', $result['title']);
					$all->MountSource($item);
				}
				
				return $all;
			
			}				

			public static function CreateCategory(){
				$parent1 = new Category();
				$parent1->Title('Автосалон');
				$parent1->Link('/cars/');
				$parent1->CatParent(0);
				$parent1->Hidden(SHOW_ALL);
				$parent1->Catorder(10);
				$parent1->Save();
				
					$cat11 = new Category();
					$cat11->Title('Автомобили в салоне');
					$cat11->Link('cars/salon');
					$cat11->CatParent($parent1->Id());
					$cat11->Hidden(SHOW_ALL);
					$cat11->Catorder(11);
					$cat11->Save();
					
					
					$cat12 = new Category();
					$cat12->Title('Автомобили на заказ');
					$cat12->Link('cars/asorder');
					$cat12->CatParent($parent1->Id());
					$cat12->Hidden(SHOW_ALL);
					$cat12->Catorder(12);
					$cat12->Save();
					
				$parent2 = new Category();
				$parent2->Title('Объявления');
				$parent2->Link('/admin/cars/');
				$parent2->CatParent(0);
				$parent2->Hidden(SHOW_ALL);
				$parent2->Catorder(20);
				$parent2->Save();
				
					/*$cat21 = new Category();
					$cat21->Title('купить авто');
					$cat21->Link('admin/cars/by');
					$cat21->CatParent($parent2->Id());
					$cat21->Hidden(SHOW_ALL);
					$cat21->Catorder(21);
					$cat21->Save();*/
					
					$cat22 = new Category();
					$cat22->Title('продать авто');
					$cat22->Link('admin/cars/add');
					$cat22->CatParent($parent2->Id());
					$cat22->Hidden(SHOW_ALL);
					$cat22->Catorder(22);
					$cat22->Save();
				
				$parent3 = new Category();
				$parent3->Title('Техцентр');
				$parent3->Link('/tehcenter/');
				$parent3->CatParent(0);
				$parent3->Hidden(SHOW_ALL);
				$parent3->Catorder(30);
				$parent3->Save();
				
				$parent4 = new Category();
				$parent4->Title('Услуги');
				$parent4->Link('/service/');
				$parent4->CatParent(0);
				$parent4->Hidden(SHOW_ALL);
				$parent4->Catorder(40);
				$parent4->Save();
				
				$parent5 = new Category();
				$parent5->Title('');
				$parent5->Link('/');
				$parent5->CatParent(0);
				$parent5->Hidden(SHOW_ALL);
				$parent5->Catorder(50);
				$parent5->Save();
				
					$cat52 = new Category();
					$cat52->Title('FAQ');
					$cat52->Link('faq');
					$cat52->CatParent($parent5->Id());
					$cat52->Hidden(SHOW_ALL);
					$cat52->Catorder(52);
					$cat52->Save();
					
					$cat53 = new Category();
					$cat53->Title('Неотвеченные вопросы');
					$cat53->Link('admin/questions');
					$cat53->CatParent($parent5->Id());
					$cat53->Hidden(SHOW_ONLY_ADMIN);
					$cat53->Catorder(53);
					$cat53->Save();
					
					$cat53 = new Category();
					$cat53->Title('Неопубликовнные объявления');
					$cat53->Link('admin/mycars');
					$cat53->CatParent($parent5->Id());
					$cat53->Hidden(SHOW_ONLY_ADMIN);
					$cat53->Catorder(54);
					$cat53->Save();
					
					$cat53 = new Category();
					$cat53->Title('Мои объявления');
					$cat53->Link('admin/mycars');
					$cat53->CatParent($parent5->Id());
					$cat53->Hidden(SHOW_ALL);
					$cat53->Catorder(55);
					$cat53->Save();
			}

	}

?>